package com.xiaoxin.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxin.dubbo.mappers.UserInfoMapper;
import com.xiaoxin.model.domain.UserInfo;
import org.springframework.util.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @author xiaoxiaode
 * @date 2021-08-29-13:26
 **/

@DubboService
public class UserInfoImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo info) {
        QueryWrapper wrapper = new QueryWrapper();
        // 用户id列表
        wrapper.in("id", userIds);
        // 判断
        if (info != null) {
            if (info.getAge() != null) {
                wrapper.lt("age", info.getAge());
            }
            if (!StringUtils.isEmpty(info.getGender())) {
                wrapper.eq("gender", info.getGender());
            }
            if (!StringUtils.isEmpty(info.getNickname())) {
                wrapper.like("nickname", info.getNickname());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(wrapper);
        // 转换为map集合,用userInfo的id获取userInfo对象
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    @Override
    public IPage getUsers(Integer page, Integer pagesize) {
        Page page2 = userInfoMapper.selectPage(new Page<UserInfo>(page,pagesize), null);
        return page2;
    }
}
